package com.atguigu.linklist;

public class Merge {
    public static void main(String[] args) {
        ListNode list1=new ListNode(1);
        ListNode list2=new ListNode(2);
        ListNode node=merge(list1, list2);
        while(node!=null){
            System.out.println(node.val);
            node=node.next;
        }
    }
    public static ListNode merge(ListNode list1,ListNode list2){
        if(list1==null && list2!=null){
            return list2;
        }
        if(list1!=null && list2==null){
            return list1;
        }
        if(list1==null && list2==null){
            return null;
        }
        ListNode left=list1;
        ListNode right=list2;
        ListNode dummy=new ListNode(-1);
        ListNode cur=dummy;
        while(left!=null && right!=null){
            if(left.val<right.val){
                dummy.next=left;
                dummy=dummy.next;
                left=left.next;
            }else{
                dummy.next=right;
                dummy=dummy.next;
                right=right.next;
            }
        }
        while(left!=null){
            dummy.next=left;
            dummy=dummy.next;
            left=left.next;
        }
        while(right!=null){
            dummy.next=right;
            dummy=dummy.next;
            right=right.next;
        }
        return cur.next;
    }
}
